<?php
namespace app\index\controller;

use wechatenterprise\webhook\WechatRobot;
use \think\Log;
use think\Request;

class Index extends \think\Controller
{
    public function index()
    {
        $X_Git_Signature = Request::instance()->header('x-coding-signature');
        $json_content =file_get_contents("php://input");
        // 进行签名解析
        $sha1 = hash_hmac("sha1", $json_content, config('weichat_entper_api_setting.TOKEN'));
        $calculate_signature = 'sha1='. $sha1;

        $X_Git_Event = Request::instance()->header('x-coding-event');
        if ($calculate_signature === $X_Git_Signature) {
            $gitWebHookBean = json_decode($json_content, true);
            switch ($X_Git_Event) {
                case "push":
                    // [backend]: 吴超 推送了 1 个提交到 refs/heads/master
                    // 7cd1e48: 更新文件 readme.md - 吴超
                    $tpl = "\[[%s](%s)\]: [%s](%s) 推送了 %u 个提交到 [%s](%s)\n";
                    $itemTpl = "> [%s](%s): %s\n";
                    $user = $gitWebHookBean['sender']['name'];
                    $userUrl = $gitWebHookBean['sender']['html_url'];
                    $commitCount = count($gitWebHookBean['commits']);
                    $repository = $gitWebHookBean['repository']['name'];
                    $repositoryUrl = $gitWebHookBean['repository']['clone_url'];
                    $ref = $gitWebHookBean['ref'];
                    $refUrl = 'https://jukejia.coding.net/p/' . $repository . '/d/' . $repository . '/git/tree/' . substr($ref, strrpos($ref, '/') + 1);
                    $msgTpl = sprintf($tpl, $repository, $repositoryUrl, $user, $userUrl, $commitCount, $ref, $refUrl);
                    foreach ($gitWebHookBean['commits'] as $key => $val) {
                        $msgTpl .= sprintf($itemTpl, substr($val['id'], 0, 7), 'https://jukejia.coding.net/p/' . $repository . '/d/' . $repository . '/git/commit/' . $val['id'], $val['message']);
                    }
                    break;
                case "merge request":
                    switch ($gitWebHookBean['action']) {
                        case 'refuse':
                            $tpl = "\[[%s](%s)\]: [%s](%s) 关闭了合并请求 [%s merge to %s](%s)\n> %s\n";
                            break;
                        case 'create':
                            $tpl = "\[[%s](%s)\]: [%s](%s) 发起了合并请求 [%s merge to %s](%s)\n> %s\n";
                            break;
                        default:
                            break;
                    }
                    if ($tpl) {
                        // [backend]: 吴超 推送了 1 个提交到 refs/heads/master
                        // 7cd1e48: 更新文件 readme.md - 吴超
                        $user = $gitWebHookBean['sender']['name'];
                        $userUrl = $gitWebHookBean['sender']['html_url'];
                        $head = $gitWebHookBean['mergeRequest']['head']['ref'];
                        $base = $gitWebHookBean['mergeRequest']['base']['ref'];
                        $repository = $gitWebHookBean['repository']['name'];
                        $repositoryUrl = $gitWebHookBean['repository']['clone_url'];
                        // $ref = $gitWebHookBean['ref'];
                        // $refUrl = 'https://jukejia.coding.net/p/' . $repository . '/d/' . $repository . '/git/tree/' . substr($ref, strrpos($ref, '/') + 1);
                        $msgTpl = sprintf($tpl, $repository, $repositoryUrl, $user, $userUrl, $head, $base, $gitWebHookBean['mergeRequest']['html_url'], $gitWebHookBean['mergeRequest']['title']);
                    }
                    break;
                case "ping":
                    $msgTpl = "成功收到了来自 Coding 的 ping 消息\n";
                    $msgTpl .= '> ' . $gitWebHookBean['zen'];
                    break;
                break;
            }


            $resultJson = WechatRobot::SendMsg($msgTpl, 'markdown');
            echo json_encode($resultJson);
        } else {
            echo "token error";
        }

        exit();
    }
    public function flushtoken()
    {
        $token = input("token");
        if (isset($token)) {
            cache('wechat_entper_access_token', null);
            //重新访问
        }
        echo "Excute Success";
        exit();
    }
}
